package com.hospital.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hospital.domain.BloodProductType;
import com.hospital.domain.HBloodApplyRecord;
import com.hospital.domain.Hospital;
import com.hospital.service.BloodProductTypeService;
import com.hospital.service.HBloodApplyRecordService;
import com.hospital.service.HospitalService;
import com.hospital.utils.R;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.*;

@Slf4j
@RestController
@RequestMapping("/hBloodApplyRecord")
@CrossOrigin(origins = "*",maxAge = 3600)
public class HBloodApplyRecordController {

    @Autowired
    private HBloodApplyRecordService hBloodApplyRecordService;

    @Autowired
    private HospitalService hospitalService;

    @Autowired
    private BloodProductTypeService bloodProductTypeService;

//    @Cacheable(value = "hBloodApplyRecord",key = "#page+'-'+#size")
    @GetMapping("/bloodTransmit")
    public R list(){
        LambdaQueryWrapper<HBloodApplyRecord> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.select(HBloodApplyRecord::getHospitalId,HBloodApplyRecord::getVolume);
        List<Map<String,Object>> list = hBloodApplyRecordService.listMaps(queryWrapper);
        for (Map<String,Object> map : list){
            int hospitalId = (int) map.get("hospital_id");
            Hospital hospital = hospitalService.getById(hospitalId);
            map.put("hospitalName",hospital.getName());
            map.remove("hospital_id");
        }

        Map<String, BigDecimal> hospitalVolumeMap = new HashMap<>();

        for (Map<String, Object> map : list) {
            String hospitalName = (String) map.get("hospitalName");
            BigDecimal volume = (BigDecimal) map.get("volume");
            hospitalVolumeMap.put(hospitalName, hospitalVolumeMap.getOrDefault(hospitalName, BigDecimal.ZERO).add(volume));
        }

        List<Map<String, Object>> result = new ArrayList<>();
        for (Map.Entry<String, BigDecimal> entry : hospitalVolumeMap.entrySet()) {
            Map<String, Object> mergedRecord = new HashMap<>();
            mergedRecord.put("hospitalName", entry.getKey());
            mergedRecord.put("volume", entry.getValue());
            result.add(mergedRecord);
        }
        return R.ok().put("data",result);
    }

//    @Cacheable(value = "hBloodApplyRecord",key = "#page+'-'+#size")
    @GetMapping("/selectPage")
    public R selectPage(@RequestParam(defaultValue = "1") Integer pageNum,
                        @RequestParam(defaultValue = "3") Integer pageSize){
        Page<HBloodApplyRecord> page = new Page<>(pageNum, pageSize);
        Page<HBloodApplyRecord> hBloodApplyRecordPage = hBloodApplyRecordService.page(page);
        List<HBloodApplyRecord> records = hBloodApplyRecordPage.getRecords();
        for (HBloodApplyRecord record : records){
            int hospitalId = record.getHospitalId();
            Hospital hospital = hospitalService.getById(hospitalId);
            int bloodProductTypeId = record.getBloodProductTypeId();
            BloodProductType type = bloodProductTypeService.getById(bloodProductTypeId);
            record.setHospitalName(hospital.getName());
            record.setBloodProductName(type.getProductName());
        }
        int total = hBloodApplyRecordService.count();

        return R.ok().put("data",records).put("total",total);
    }

    @GetMapping("/hospital")
    public R hospital(@RequestParam(defaultValue = "1") Integer pageNum,
                        @RequestParam(defaultValue = "3") Integer pageSize)  {
        Page<Hospital> page = new Page<>(pageNum,pageSize);
        Page<Hospital> hospitalPage = hospitalService.page(page);
        List<Hospital> records = hospitalPage.getRecords();
        return R.ok().put("list",records);
    }

//    @Cacheable(value = "hBloodApplyRecord",key = "#page+'-'+#size")
    @GetMapping("bloodApply")
    public R bloodApply(){
        List<HBloodApplyRecord> list = hBloodApplyRecordService.list();
        Double[] mount = new Double[7];
        Arrays.fill(mount, 0.0);
        for (HBloodApplyRecord type : list){
            if(type.getHospitalId() != 1) continue;
            Date date = type.getTransfusionDate();
            SimpleDateFormat monthFormat = new SimpleDateFormat("MM");
            String month = monthFormat.format(date);
            if (month.equals("05")) mount[0]+=type.getVolume();
            else if (month.equals("06")) mount[1]+=type.getVolume();
            else if (month.equals("07")) mount[2]+=type.getVolume();
            else if (month.equals("08")) mount[3]+=type.getVolume();
            else if (month.equals("09")) mount[4]+=type.getVolume();
            else if (month.equals("10")) mount[5]+=type.getVolume();
            else if (month.equals("11")) mount[6]+=type.getVolume();
        }
        return R.ok().put("data",mount);
    }

//    @Cacheable(value = "hBloodApplyRecord",key = "#page+'-'+#size")
    @GetMapping("bloodApplyAll")
    public R bloodApplyAll(){
        List<HBloodApplyRecord> list = hBloodApplyRecordService.list();
        Double[] mount = new Double[7];
        Arrays.fill(mount, 0.0);
        for (HBloodApplyRecord type : list){
            Date date = type.getTransfusionDate();
            SimpleDateFormat monthFormat = new SimpleDateFormat("MM");
            String month = monthFormat.format(date);
            if (month.equals("05")) mount[0]+=type.getVolume();
            else if (month.equals("06")) mount[1]+=type.getVolume();
            else if (month.equals("07")) mount[2]+=type.getVolume();
            else if (month.equals("08")) mount[3]+=type.getVolume();
            else if (month.equals("09")) mount[4]+=type.getVolume();
            else if (month.equals("10")) mount[5]+=type.getVolume();
            else if (month.equals("11")) mount[6]+=type.getVolume();
        }
        return R.ok().put("data",mount);
    }

//    @CachePut(value = "hBloodApplyRecord",key = "#page+'-'+#size")
    @PostMapping("save")
    public R save(@RequestBody HBloodApplyRecord hBloodApplyRecord){
        log.info(hBloodApplyRecord.toString());
        String hospitalName = hBloodApplyRecord.getHospitalName();
        Hospital hospital = hospitalService.getOne(new QueryWrapper<Hospital>().eq("name",hospitalName));
        hBloodApplyRecord.setHospitalId(hospital.getHospitalId());

        String bloodProductType = hBloodApplyRecord.getBloodProductName();
        BloodProductType type = bloodProductTypeService.getOne(new QueryWrapper<BloodProductType>().eq("product_name",bloodProductType));
        hBloodApplyRecord.setBloodProductTypeId(type.getBloodProductTypeId());

        hBloodApplyRecordService.save(hBloodApplyRecord);
        return R.ok();
    }

//    @CacheEvict(value = "hBloodApplyRecord",allEntries = true)
    @DeleteMapping("delete/{id}")
    public R delete(@PathVariable("id") Integer id){
        hBloodApplyRecordService.removeById(id);
        return R.ok();
    }
}
